package com.spring.lo.framework.factory.support;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 默认的单例Bean注册中心
 * @author zhangbh
 * 2023-05-28 17:42
 **/
public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {

    /**
     * 存放单例bean容器
     * key=beanName,value=Bean实例
     */
    private Map<String, Object> singletonObjects = new HashMap<>();

    @Override
    public void registerSingleton(String beanName, Object singletonBean) {
        singletonObjects.put(beanName, singletonBean);
    }

    @Override
    public Object getSingleton(String beanName) {
        return singletonObjects.get(beanName);
    }

    @Override
    public List<String> getSingletonNames() {
        Iterator<Map.Entry<String, Object>> it = singletonObjects.entrySet().iterator();
        List<String> names = new ArrayList<>(singletonObjects.size());
        while (it.hasNext()) {
            names.add(it.next().getKey());
        }
        return names;
    }
}
